The DataList control is used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default.The DataList control may be bound to a database table, an XML file, or another list of items.
<asp:DataListID="DataList1"runat="server"> </asp:DataList>
Bind Data in DataList:
<asp:DataListID="DataList1"runat="server"
oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:LabelID="lbl1"runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "id") %> '></asp:Label>
<%#DataBinder.Eval(Container.DataItem, "pass") %>
<%#DataBinder.Eval(Container.DataItem, "name") %>
<asp:LinkButtonid="LinkButton1"runat="server"Text="edit"CommandName="edit"/>
<asp:LinkButtonid="LinkButton2"runat="server"Text="Delete"
CommandName="Delete"/>
</ItemTemplate>
<EditItemTemplate>
Item: <asp:TextBoxid="Text1"runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "id") %>'/>
Item: <asp:TextBoxid="Text2"runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "pass") %>'/>
Item: <asp:TextBoxid="Text3"runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "name") %>'/>
<asp:LinkButtonid="button1"runat="server"Text="Update"CommandName="update"/>
<asp:LinkButtonid="button2"runat="server"Text="Cancel"CommandName="cancel"/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBoxID="txt1"runat="server"></asp:TextBox>
<asp:TextBoxID="txt2"runat="server"></asp:TextBox>
<asp:TextBoxID="txt3"runat="server"></asp:TextBox>
<asp:LinkButtonID="lnkbtn1"runat="server"CommandName="ADD">ADD</asp:LinkButton>
</FooterTemplate>
</asp:DataList>
<asp:LabelID="lbl1"runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "id") %> '></asp:Label>
<%#DataBinder.Eval(Container.DataItem, "pass") %>
<%#DataBinder.Eval(Container.DataItem, "name") %>
DataBinder.Eval takes the argument. The first arg is the data object to bind to. The second arg is the string name of the field which will display on the page and return the data corresponding to the second argument.
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Binddata();// this function is called when the page is loaded.
}
}
public void Binddata()
{
DataSet ds = new DataSet();
DataList1.DataSource = Class1.execute_fill_datalist("insert_reg");
// Here data is fetched from the database through stored procedure(insert_reg).
DataList1.DataBind();
// Here data is bind to the control and show on page
}
In above code execute_fill_datalist is a function created in Class1.cs which is described below.
Andinsert_reg is a stored procedure made in backend.Which is described below
DataList Event
Editing DataList Items
The DataList control supports in-place editing of the data in an item through its EditItemTemplate property. The EditItemTemplate defines the content and appearance of the item when it is being edited.
// Edit command is used to editing in DataList
protected void DataList1_EditCommand(object source,DataListCommandEventArgse) {
//Set the EditItemIndex property to the index of the item clicked
// in the DataList control to enable editing for that item.
// Rebind the DataList to the data source to refresh the control.
DataList1.EditItemIndex = (int)e.Item.ItemIndex;
Binddata();
}
// CancelCommand is used to cancel editing.
protectedvoid DataList1_CancelCommand(objectsource,DataListCommandEventArgs
e)
{
// Set the EditItemIndex property to -1 to exit editing mode.And rebind the data.
DataList1.EditItemIndex = -1;
Binddata();
}
Add, Update and Delete Data in DataList:
The Item Command event is raised whenever any button associated with a row in the DataList is clicked. This provides for programmatically determining which specific command button is clicked and take appropriate action.
Here this event is used for Adding data
protected void DataList1_ItemCommand(object source,DataListCommandEventArgse) {
if (e.CommandName == "ADD")
{
// Here FindControl will find the TextboxId in which adding will be done.
string i = ((TextBox)e.Item.FindControl("txt1")).Text;
string ps = ((TextBox)e.Item.FindControl("txt2")).Text;
string nm = ((TextBox)e.Item.FindControl("txt3")).Text;
Class1.update_reg("add_reg", i, ps, nm);
DataList1.EditItemIndex = -1;
Binddata();
}
}
// UpdateCommand is used to update data
protectedvoid DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
// Here FindControl will find the Textbox in which updating will be done.
string i = ((TextBox)e.Item.FindControl("Text1")).Text;
string ps = ((TextBox)e.Item.FindControl("Text2")).Text;
string nm = ((TextBox)e.Item.FindControl("Text3")).Text;
// update_reg is a function which fetch the data from database through stored procedure.
Class1.update_reg("update_reg",i,ps, nm);
DataList1.EditItemIndex = -1;
Binddata();
}
// DeleteCommand is used to delete data
protectedvoid DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string dl = ((Label)e.Item.FindControl("lbl1")).Text;
Class1.Delete_reg("del_reg", dl);
Binddata();
}
Add both namespace in every .cs page in which you want to use data and SQL related keyword
using System.Data;
using System.Data.SqlClient;
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Web;
publicclassClass1
{
publicstaticDataSet execute_fill_datalist(string spname)// this function is used to bind data
{
SqlConnection con = Connection.CreateConnection();
DataSet ds = newDataSet();
SqlCommand cmd = newSqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
// connection is open
SqlDataAdapter da = newSqlDataAdapter(spname, con);
da.Fill(ds);
return ds;
}
publicstaticvoid updat_reg(string spname,string id,string ps,string nm)// this function is used to update and add data
{
SqlConnection con=Connection.CreateConnection();
SqlCommand cmd = newSqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = ps;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = nm;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
publicstaticvoid delete_regform(string spname, string id) // this function is used to delete data
{
SqlConnection con = Connection.CreateConnection();
SqlCommand cmd = newSqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
}
}
In class1.cs Connection keyword is used
Connection is class in whichCreateConnection function is created.
Connection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Web;
using System.Data;
using System.Data.SqlClient;
publicclassConnection
{
publicstaticSqlConnection CreateConnection()
{
string connectionstring;
connectionstring = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(connectionstring);
return sqlcon; // Here connection string will be return
}
}
mycon is connection object created in web.config
web.config:
<connectionStrings>
<addname="mycon" connectionString="Data Source=(local);Initial Catalog=my; User Id=sa; Password=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
Write this connection string above <system.web>
Stored procedure:
This procedure is used to select data
Createprocedure insert_reg
as
begin
select*from regform
end
This procedure is used to Add data
Create procedure add_reg
@id varchar(50),
@pass varchar(50),
@name varchar(50)
as
begin
insertinto regform
(id,pass,name
)
values
(
@id,
@pass,
@name
)
End
This procedure is used to Delete data
createprocedure del_reg
@id varchar(50)
as
begin
delete from regform where id=@id
end
This procedure is used to Update data
createprocedure [dbo].[update_reg]
@id varchar(50),
@pass varchar(50),
@name varchar(50)
as
begin
update regform set pass=@pass,name=@name where id=@id
end
Anonymous User
22-Feb-2019Thank You for the help.
Samuel Fernandes
19-Jul-2017Your content is really informative to read.